import os

from tkinter import *
from PyQt4 import QtGui, QtCore
from QtGui import QMainWindow

class MainWindow(QMainWindow):
  def __init__(self):
    QMainWindow.__init__(self)

    self.setupUi(self)

    self.dbNewBtn.clicked.connect(self.new_db)
    self.dbCreateBtn.clicked.connect(self.create_db)
    self.dbLoadBtn.clicked.connect(self.load_db)

    self.dbView.itemChanged.connect(self.db_item_changed)
    self.dbView.itemClicked.connect(self.db_item_selected)
    self.dbView.setIconSize(QtCore.QSize(30, 30))
    
    self.imgListViewBtn.clicked.connect(self.img_show_list_view)
    self.imgGridViewBtn.clicked.connect(self.img_show_grid_view)
    self.imgTreeViewBtn.clicked.connect(self.img_show_tree_view)

    self.imgAddBtn.clicked.connect(self.add_images)
    self.imgFolderAddBtn.clicked.connect(self.add_image_folder)
    self.thumbnailListWidget.setIconSize(QtCore.QSize(60, 60))

    self.default_db_dir = ''
    self.default_db_name = 'New Database'
    self.default_db_icon = QtGui.QIcon('default.jpg')

    self.default_img_dir = '' 
    self.img_model = ''
    self.img_files = []

  def update_img_view(self): 
    # pass these filenames to db and add them to db
    for path in self.img_files:
      print(path)
      # add to current db, and get a img_id
      img = QtGui.QIcon(path)
      filename = path.split(os.sep)[-1]
      item = QtGui.QListWidgetItem(img, filename, self.thumbnailListWidget)
      QtCore.QCoreApplication.processEvents()
      #item.setData(img_id & filepath)

  def add_images(self):
    print('add images')
    options = QtGui.QFileDialog.Options()
    files = QtGui.QFileDialog.getOpenFileNames(self, 
        'Select image files', self.default_img_dir,
        'All Files (*);;Text Files (*.txt)', options)
    if files:
      self.default_img_dir = files[0]
      self.img_files.extend(files)
      self.update_img_view()
  
  def add_image_folder(self):
    print('add image folder')
    options = (QtGui.QFileDialog.DontResolveSymlinks | 
               QtGui.QFileDialog.ShowDirsOnly)
    directory = QtGui.QFileDialog.getExistingDirectory(self,
        'Create database from directory', self.default_db_dir, options)
    if directory:
      self.default_img_dir = directory
      for file in os.listdir(directory):
        self.img_files.append(os.path.join(directory, file))
      self.update_img_view()

  def img_show_tree_view(self):
    print('show tree view')

  def img_show_list_view(self):
    print('show list view')
    size_policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding,
                                    QtGui.QSizePolicy.Fixed)
    #self.thumbnailListWidget.setSizePolicy(size_policy)
    self.thumbnailListWidget.setViewMode(QtGui.QListView.ListMode)

  def img_show_grid_view(self):
    print('show grid view')
    size_policy = QtGui.QSizePolicy(QtGui.QSizePolicy.Fixed,
                                    QtGui.QSizePolicy.Fixed)
    #self.thumbnailListWidget.setSizePolicy(size_policy)
    self.thumbnailListWidget.setViewMode(QtGui.QListView.IconMode)


  def db_item_changed(self, item):
    print(item.text())

  def db_item_selected(self, item):
    db_id = item.data(QtCore.Qt.UserRole)
    print('db id: %d, db name: %s' % (db_id, item.text()))
    # fetch db data, and display

  def new_db(self):
    print('add database')
    # create a new db, and get db id
    db_id = 123
    item = QtGui.QListWidgetItem(self.default_db_icon, self.default_db_name, 
                                 self.dbView) 
    item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)
    item.setData(QtCore.Qt.UserRole, db_id)
    #self.dbView.addItem(item)

  def create_db(self):
    print('create database')
    options = (QtGui.QFileDialog.DontResolveSymlinks | 
               QtGui.QFileDialog.ShowDirsOnly)
    directory = QtGui.QFileDialog.getExistingDirectory(
        self, 'Select a directory containing image files', 
        self.default_db_dir, options)
    if directory:
      self.default_db_dir = directory
      #load files

  def load_db(self):
    print('load database')
    options = QtGui.QFileDialog.Options()
    files = QtGui.QFileDialog.getOpenFileNames(self, 
        'Select database files', self.default_db_dir,
        'All Files (*);;Text Files (*.txt)', options)
    if files:
      self.default_db_dir = files[0]
      # pass these filenames to db manager to parse and get db information
      for filename in files:
        print(filename)

app = QtGui.QApplication(sys.argv)

window = MainWindow()
window.show()

sys.exit(app.exec_())
